package ar.republicagroup.gestion.dao.jpa;

import ar.republicagroup.gestion.dao.CodigoPostalDAO;
import ar.republicagroup.gestion.entities.CodigoPostal;
import ar.republicagroup.gestion.util.JPAUtil;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.apache.log4j.Logger;

public class CodigoPostalDAOImplJPA implements CodigoPostalDAO {
	
        public static final String ORIGEN_CAP_BSAS = "CAP";
        public static final String ORIGEN_PROVINCIAS = "VC";
    
	
		private EntityManager em;
                Logger log = Logger.getLogger(CodigoPostalDAOImplJPA.class);
		
		
		public CodigoPostalDAOImplJPA(){
			this.em = JPAUtil.getEntityManager();	
		}
		
		

	@Override
	public List<CodigoPostal> findEntities() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String insert(CodigoPostal beanCodigoPostal) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String update(CodigoPostal beanCodigoPostal) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public CodigoPostal get(Integer idcodigopostal) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String delete(Integer idcodigopostal) {
		// TODO Auto-generated method stub
		return null;
	}

	
	@Override
	public List<CodigoPostal> findCodigoPostalByCodigo(Integer codigopostal) {
		
		List<CodigoPostal> codigoPostalList = null;
		try{
		
			Query query 	= em.createQuery("SELECT cp FROM CodigoPostal cp WHERE cp.codigo = ?1", CodigoPostal.class);
			query.setParameter(1, codigopostal);
			codigoPostalList 		= (List<CodigoPostal>)query.getResultList();
		
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		return codigoPostalList;
		
	}

    @Override
    public String getOrigenOfCodigoPostalByCodigo(Integer codigopostal) {
            String origen = ORIGEN_PROVINCIAS;
            
            List<CodigoPostal> codigoPostalList = null;
		try{
		
			Query query                     = em.createQuery("SELECT cp FROM CodigoPostal cp WHERE cp.codigo = ?1", CodigoPostal.class);
			query.setParameter(1, codigopostal);
			codigoPostalList 		= (List<CodigoPostal>)query.getResultList();
		
		}catch(Exception ex){
			log.error(ex.getMessage());
		}
                
                if (codigoPostalList.size() > 0){
                    origen = ORIGEN_CAP_BSAS;
                }
                
                return origen;
        
    }

}
